"""
商品相关的Pydantic模型
"""

from pydantic import BaseModel, Field, HttpUrl
from typing import Optional, List
from datetime import datetime
from decimal import Decimal


class ProductBase(BaseModel):
    """商品基础模型"""
    name: str = Field(..., description="商品名称")
    price: Decimal = Field(..., ge=0, description="商品价格")
    url: HttpUrl = Field(..., description="商品链接")
    image_url: Optional[HttpUrl] = Field(None, description="商品图片链接")
    description: Optional[str] = Field(None, description="商品描述")
    category: Optional[str] = Field(None, description="商品分类")
    condition: Optional[str] = Field(None, description="商品状态")
    seller: Optional[str] = Field(None, description="卖家")
    location: Optional[str] = Field(None, description="商品位置")


class ProductCreate(ProductBase):
    """创建商品模型"""
    external_id: str = Field(..., description="外部商品ID")
    source: str = Field(default="mercari", description="商品来源")


class ProductUpdate(BaseModel):
    """更新商品模型"""
    name: Optional[str] = Field(None, description="商品名称")
    price: Optional[Decimal] = Field(None, ge=0, description="商品价格")
    url: Optional[HttpUrl] = Field(None, description="商品链接")
    image_url: Optional[HttpUrl] = Field(None, description="商品图片链接")
    description: Optional[str] = Field(None, description="商品描述")
    category: Optional[str] = Field(None, description="商品分类")
    condition: Optional[str] = Field(None, description="商品状态")
    seller: Optional[str] = Field(None, description="卖家")
    location: Optional[str] = Field(None, description="商品位置")
    is_available: Optional[bool] = Field(None, description="是否可用")


class ProductResponse(ProductBase):
    """商品响应模型"""
    id: int
    external_id: str
    source: str
    is_available: bool
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


class ProductListResponse(BaseModel):
    """商品列表响应模型"""
    products: List[ProductResponse]
    total: int
    page: int
    page_size: int


class ProductSearchRequest(BaseModel):
    """商品搜索请求模型"""
    keyword: str = Field(..., min_length=1, max_length=100, description="搜索关键词")
    min_price: Optional[Decimal] = Field(None, ge=0, description="最低价格")
    max_price: Optional[Decimal] = Field(None, ge=0, description="最高价格")
    category: Optional[str] = Field(None, description="商品分类")
    condition: Optional[str] = Field(None, description="商品状态")
    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=20, ge=1, le=100, description="每页数量")


class ProductSearchResponse(BaseModel):
    """商品搜索响应模型"""
    products: List[ProductResponse]
    total: int
    page: int
    page_size: int
    keyword: str
    search_time: datetime


class ProductNotificationData(BaseModel):
    """商品通知数据模型"""
    product_id: int
    product_name: str
    product_price: Decimal
    product_url: HttpUrl
    product_image_url: Optional[HttpUrl] = None
    monitor_keyword: str
    notification_type: str = Field(default="new_product", description="通知类型")


class ProductStats(BaseModel):
    """商品统计模型"""
    total_products: int
    available_products: int
    unavailable_products: int
    categories: List[str]
    price_range: dict
